package cn.iocoder.yudao.module.handguard.controller.admin.labeltable;

import cn.iocoder.yudao.module.handguard.convert.labeltable.LabelTableConvert;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.admin.labeltable.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.labeltable.LabelTableDO;
import cn.iocoder.yudao.module.handguard.service.labeltable.LabelTableService;

@Tag(name = "管理后台 - 标签")
@RestController
@RequestMapping("/hutai/label-table")
@Validated
public class LabelTableController {

    @Resource
    private LabelTableService labelTableService;

    @PostMapping("/create")
    @Operation(summary = "创建标签")
    @PreAuthorize("@ss.hasPermission('hutai:label-table:create')")
    public CommonResult<Long> createLabelTable(@Valid @RequestBody LabelTableCreateReqVO createReqVO) {
        return success(labelTableService.createLabelTable(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新标签")
    @PreAuthorize("@ss.hasPermission('hutai:label-table:update')")
    public CommonResult<Boolean> updateLabelTable(@Valid @RequestBody LabelTableUpdateReqVO updateReqVO) {
        labelTableService.updateLabelTable(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除标签")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hutai:label-table:delete')")
    public CommonResult<Boolean> deleteLabelTable(@RequestParam("id") Long id) {
        labelTableService.deleteLabelTable(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得标签")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hutai:label-table:query')")
    public CommonResult<LabelTableRespVO> getLabelTable(@RequestParam("id") Long id) {
        LabelTableDO labelTable = labelTableService.getLabelTable(id);
        return success(LabelTableConvert.INSTANCE.convert(labelTable));
    }

    @GetMapping("/list")
    @Operation(summary = "获得标签列表")
    @PreAuthorize("@ss.hasPermission('hutai:label-table:query')")
    public CommonResult<List<LabelTableRespVO>> getLabelTableList() {
        List<LabelTableDO> list = labelTableService.getLabelTableList();
        return success(LabelTableConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得标签分页")
    @PreAuthorize("@ss.hasPermission('hutai:label-table:query')")
    public CommonResult<PageResult<LabelTableRespVO>> getLabelTablePage(@Valid LabelTablePageReqVO pageVO) {
        PageResult<LabelTableDO> pageResult = labelTableService.getLabelTablePage(pageVO);
        return success(LabelTableConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出标签 Excel")
    @PreAuthorize("@ss.hasPermission('hutai:label-table:export')")
    @OperateLog(type = EXPORT)
    public void exportLabelTableExcel(@Valid LabelTableExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<LabelTableDO> list = labelTableService.getLabelTableList(exportReqVO);
        // 导出 Excel
        List<LabelTableExcelVO> datas = LabelTableConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "标签.xls", "数据", LabelTableExcelVO.class, datas);
    }

}
